package com.qzdsoft.erpcloud.controller.com;

import java.text.ParseException;
import java.util.List;

import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.qzdsoft.erpcloud.controller.BaseController;
import com.qzdsoft.erpcloud.domain.basic.Currency;
import com.qzdsoft.erpcloud.domain.com.CustomerSupplierStatement;
import com.qzdsoft.erpcloud.service.basic.CurrencyService;
import com.qzdsoft.erpcloud.service.com.CustomerSupplierStatementService;
import com.qzdsoft.erpcloud.service.sys.Permission;
import com.qzdsoft.erpcloud.service.sys.SysLog;
import com.qzdsoft.erpcloud.vo.com.CustomerSupplierStatementInfo;
import com.qzdsoft.erpcloud.vo.com.CustomerSupplierStatementQueryParam;
import com.qzdsoft.utils.ExceptionUtil;
import com.qzdsoft.vo.LayTableResponse;
import com.qzdsoft.vo.Response;
import com.qzdsoft.vo.com.StatementType;

/**
 * 供应商结算方式
 * @author pc-20170422
 *
 */
@Controller
@RequestMapping("/com/provider/supplier/statement")
public class ProviderSupplierStatementController extends BaseController {
	 private static final Logger logger = LoggerFactory.getLogger(ProviderSupplierStatementController.class);
	 private static final String BASEPATH = "com/providerstatement";
	 
	 @Autowired
	 private CustomerSupplierStatementService comCustomerSupplierStatementService;
	 @Autowired
	 private  CurrencyService  basicCurrencyService;
	 
	 
	 @Permission(validate=false)
	    @RequestMapping("/index")
	    public String index(Model model,Integer id,String companyName,Integer companyId) {
	        model.addAttribute("typeList", StatementType.ALL);
	        model.addAttribute("companyId", companyId);
	        model.addAttribute("supplierCustomerId", id);
//	        return "com/customer_supplier_statement";
	        return BASEPATH+"/index";
	    }
	    @ResponseBody
	    @RequestMapping("/data")
	    public LayTableResponse<CustomerSupplierStatementInfo> data(HttpServletRequest request, CustomerSupplierStatementQueryParam param) throws ParseException {

			if(param.isFirst()) {
				return LayTableResponse.nullDataPage();
			}
//	        Integer count = comCustomerSupplierStatementService.selectDataCount(param.getKeyStr(), param.getType(), param.getTimeFrom(), param.getTimeTo(),param.getSupplierCustomerId(),param.getCompanyId());
//	        List list = comCustomerSupplierStatementService.selectData(param.getKeyStr(), param.getType(), param.getTimeFrom(), param.getTimeTo(), param.getiDisplayStart(), param.getiDisplayLength(),param.getSupplierCustomerId(),param.getCompanyId());
//	        return new DataTablePo(count, list, param.getsEcho());
			List<CustomerSupplierStatementInfo> lists = comCustomerSupplierStatementService.selectAll(param);
	    	return new LayTableResponse<CustomerSupplierStatementInfo>(lists);
	    	
	    }
	    
	    @Permission(validate=false)
	    @RequestMapping("/addinit")
	    public ModelAndView addinit(Integer companyId,Integer supplierCustomerId) {
	    	ModelAndView mv = new ModelAndView();
	    	//查询币种信息
	    	List<Currency> currency = basicCurrencyService.selectAll(companyId);
	    	//结算方式
	    	mv.addObject("statementType", StatementType.ALL);
	    	mv.addObject("companyId", companyId);
	    	mv.addObject("supplierCustomerId", supplierCustomerId);
	    	mv.addObject("currencyType",currency);
	    	mv.setViewName(BASEPATH+"/add");
	    	return mv;
	    }
	    
	    @SysLog(description="供应商结算方式新增保存")
	    @ResponseBody
	    @RequestMapping("/add")
	    public Response add(CustomerSupplierStatement comCustomerSupplierStatement) {
//	        comCustomerSupplierStatement.setCompany_id(SessionUtil.getLoggedCompanyId(request,comCustomerSupplierStatement.getCompany_id()));
//	    	comCustomerSupplierStatement.setCompany_id(1);
	    	logger.debug("结算方式新增,info={}",comCustomerSupplierStatement);
	    	try{
	    		return comCustomerSupplierStatementService.edit(comCustomerSupplierStatement);
	    	}catch(Exception ex) {
	    		ex.printStackTrace();
	    		 logger.error("新增结算方式错误，异常信息：{}",ExceptionUtil.getStackTrace(ex));
	    		 return Response.error(ex.getMessage());
	    	}
	    }
	    @Permission(validate=false)
	    @ResponseBody
	    @RequestMapping("/edit")
	    public ModelAndView edit(@RequestParam("id") Integer id,Integer companyId,Integer supplierCustomerId) {
			ModelAndView mv = new ModelAndView();
		     CustomerSupplierStatement statement = comCustomerSupplierStatementService.selectById(id);
		     //查询币种信息
		 	List<Currency> currency = basicCurrencyService.selectAll(companyId);
		 	mv.addObject("currencyType",currency);
		     	//结算方式
		 	 mv.addObject("statementType", StatementType.ALL);
		     mv.addObject("obj", statement);
		     mv.setViewName(BASEPATH+"/edit");
	         return mv;
	    }
	    
	    @SysLog(description="供应商结算方式修改保存")
	    @ResponseBody
	    @RequestMapping("/edit_execute")
	    public Object editExecute(HttpServletRequest request, CustomerSupplierStatement comCustomerSupplierStatement) {
//	        comCustomerSupplierStatement.setCompany_id(SessionUtil.getLoggedCompanyId(request,comCustomerSupplierStatement.getCompany_id()));
//	    	comCustomerSupplierStatement.setCompany_id(1);
	    	logger.debug("结算方式修改,info={}",comCustomerSupplierStatement);
	    	try{
	    		return comCustomerSupplierStatementService.edit(comCustomerSupplierStatement);
	    	}catch(Exception ex) {
	    		ex.printStackTrace();
	    		 logger.error("修改结算方式错误，异常信息：{}",ExceptionUtil.getStackTrace(ex));
	    		 return Response.error(ex.getMessage());
	    	}
	    }
	  /**
	     * 删除
	     * @param request
	     * @param id
	     * @return
	     */
	    @SysLog(description="供应商结算方式删除")
	    @ResponseBody
	    @RequestMapping("/del_execute")
	    public Object delExecute(HttpServletRequest request, @RequestParam("id") Integer id) {
	        logger.debug("客户供应商结算方式删除");
	    	return comCustomerSupplierStatementService.del(id);
	    }
	    
	    /**
	     * 详情查看
	     */
	    @Permission(validate=false)
	    @RequestMapping("/view")
	    public ModelAndView view(Integer id,Integer companyId) {
			ModelAndView mv = new ModelAndView();
		     CustomerSupplierStatement statement = comCustomerSupplierStatementService.selectById(id);
		     //查询币种信息
		 	List<Currency> currency = basicCurrencyService.selectAll(companyId);
		 	mv.addObject("currencyType",currency);
		     	//结算方式
		 	 mv.addObject("statementType", StatementType.ALL);
		     mv.addObject("obj", statement);
		     mv.setViewName(BASEPATH+"/view");
	         return mv;
	    }
	 
}
